<?php
namespace backend\models;

use tests\codeception\frontend\unit\models\ResetPasswordFormTest;
use yii\base\Model;
use common\models\Adminuser;
use yii\helpers\VarDumper;

/**
 * Signup form
 */
class ResetpwdForm extends Model
{
//    public $username;
//    public $nickname;
//    public $email;
    public $password;
    public $password_repeat;
   // public $profile;


    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
//            ['username', 'filter', 'filter' => 'trim'],
//            ['username', 'required'],
//            ['username', 'unique', 'targetClass' => '\common\models\Adminuser', 'message' => '用户名已经存在.'],
//            ['username', 'string', 'min' => 2, 'max' => 255],
//
//            ['email', 'filter', 'filter' => 'trim'],
//            ['email', 'required'],
//            ['email', 'email'],
//            ['email', 'string', 'max' => 255],
//            ['email', 'unique', 'targetClass' => '\common\models\Adminuser', 'message' => '邮件地址已经存在.'],

            ['password', 'required'],
            ['password', 'string', 'min' => 6],

            ['password_repeat','compare','compareAttribute'=>'password','message'=>'两次密码输入不一致'],
//            ['nickname','required'],
//            ['nickname','string','max'=>128],
//            ['profile','string'],
        ];
    }

    public function attributeLabels()
    {
        return [
//            'id' => 'ID',
//            'username' => '用户名',
//            'nickname' => '昵称',
            'password' => '密码',
            'password_repeat' => '重置密码',
//            'email' => 'Email',
//            'profile' => '简介',
//            'auth_key' => 'Auth Key',
//            'password_hash' => 'Password Hash',
//            'password_reset_token' => 'Password Reset Token',

        ];
    }


    /**
     * Signs user up.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function resetPassword($id)
    {
        if (!$this->validate()) {
            return null;
        }
        
        $adminuser = Adminuser::findOne($id);
        $adminuser->setPassword($this->password);
        $adminuser->removePasswordResetToken();


        //$user->save();VarDumper::dump($user->errors);exit(0);
        
        return $adminuser->save() ? true : false;
    }
}
